﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_Product_Default : System.Web.UI.Page
{
	private string pageNumber = "1";
	private int howManyPages = 1;
	// pager links format
	string firstPageUrl = "";
	string pagerFormat = "";

    protected void Page_Load(object sender, EventArgs e)
    {
		if (!IsPostBack)
		{
			this.Form.Action = "Backend/Product/Default.aspx";
			this.Title = "គ្រប់គ្រងមុខម្ហូប - " + ElegantFoodConfiguration.SiteName;

			// Retrieve Page from the query string
			pageNumber = Request.QueryString["Page"];

			if (pageNumber == null) pageNumber = "1";
			Session["PageNumber"] = pageNumber;

			GetProduct(pageNumber);

			firstPageUrl = Link.ToPage(Session["PageNumber"].ToString(), HttpContext.Current.Request.Url.AbsolutePath);
			pagerFormat = Link.ToPage("{0}", HttpContext.Current.Request.Url.AbsolutePath);

			BottomPagination.ShowPagination(Int32.Parse(Session["PageNumber"].ToString()), howManyPages, firstPageUrl, pagerFormat, false);
		}
    }

	// Get all products
	protected void GetProduct(string page)
	{
		ProductList.DataSource = EF_Product.GetProduct(page, out howManyPages);
		ProductList.DataBind();
	}

	// Delete product by id
	protected void ProductList_RowDeleting(object sender, GridViewDeleteEventArgs e)
	{
		int id = Int32.Parse(this.ProductList.Rows[e.RowIndex].Cells[1].Text);
		if (EF_Product.DeleteProduct(id))
		{
			displayMessage.Text = "Product has been deleted!";
			displayMessage.CssClass = "ef-success";
		}
		else
		{
			displayMessage.Text = "Product cannot delete!";
			displayMessage.CssClass = "ef-error";
		}
		GetProduct(Session["PageNumber"].ToString());
	}

	// Enable editing mode
	protected void ProductList_RowEditing(object sender, GridViewEditEventArgs e)
	{
		ProductList.EditIndex = e.NewEditIndex;
		GetProduct(Session["PageNumber"].ToString());
	}

	// Update product
	protected void ProductList_RowUpdating(object sender, GridViewUpdateEventArgs e)
	{
		EF_ProductProperty product = new EF_ProductProperty();
		GridViewRow gvRow = this.ProductList.Rows[e.RowIndex];
		product.ProductID = Int32.Parse(ProductList.Rows[e.RowIndex].Cells[1].Text);
		product.Name = ((TextBox)gvRow.Cells[3].Controls[0]).Text;
		product.Price = decimal.Parse(((TextBox)gvRow.Cells[4].Controls[0]).Text);
		product.Promotion = bool.Parse(((TextBox)gvRow.Cells[5].Controls[0]).Text);
		product.HightLight = bool.Parse(((TextBox)gvRow.Cells[6].Controls[0]).Text);
		product.Status = bool.Parse(((TextBox)gvRow.Cells[7].Controls[0]).Text);

		if (EF_Product.UpdateProduct(product))
		{
			ProductList.EditIndex = -1;
			GetProduct(Session["PageNumber"].ToString());
		}
	}

	// Cancel editing mode
	protected void ProductList_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
	{
		ProductList.EditIndex = -1;
		GetProduct(Session["PageNumber"].ToString());
	}
}